package com.chengjue.transaction.learn.thread.cyclebarrier;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;

/**
 * 描述:
 *
 * @author Kipeng Huang
 * @create 2018-03-03 下午2:21
 */
public class BarrierTest {

	public static void main(String[] args) {
		ThreadFactory threadFactory = new ThreadFactoryBuilder()
				.setNameFormat("demo-pool-%d")
				.build();
		ExecutorService threadPool = new ThreadPoolExecutor(3,
				20,
				2000L,
				TimeUnit.MILLISECONDS,
				new ArrayBlockingQueue<Runnable>(1024),
				threadFactory,
				new ThreadPoolExecutor.AbortPolicy());

		CyclicBarrier cyclicBarrier = new CyclicBarrier(20);

		for (int i = 0; i < 20; i++) {
			threadPool.execute(new CycleBarrierRunnable(cyclicBarrier));
		}
		cyclicBarrier.reset();
		System.out.println("主线程程序执行");
		threadPool.shutdown();

	}
}
